package com.lovo.sgproj.frame.classmanage;

import javax.swing.JDialog;

import com.lovo.netCRM.component.LovoButton;
import com.lovo.netCRM.component.LovoDate;
import com.lovo.netCRM.component.LovoTxt;
import com.lovo.sgproj.Config;
import com.lovo.sgproj.bean.ClassBean;
import com.lovo.sgproj.frame.MainFrame;
import com.lovo.sgproj.service.ClassService;
import com.lovo.sgproj.service.imp.ClassServiceImp;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ClassAddDialog extends JDialog {

    ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);

    private LovoTxt classTxt;
    private LovoTxt teacherTxt;
    private LovoDate classOpenTime;
    private LovoButton addBtn;
    private LovoButton cancelBtn;

    private ClassBean theClass;

    private MainFrame frame;

    public ClassAddDialog(MainFrame frame) {
        super(frame, "学生换房", true);

        this.frame = frame;

        this.setBounds(550, 300, 300, 280);
        this.setLayout(null);
        this.init();

        this.setVisible(true);
    }

    private void init() {
        // TODO Auto-generated method stub
        this.classTxt = new LovoTxt("开班名称", 20, 20, this);
        this.teacherTxt = new LovoTxt("带班老师", 20, 80, this);
        this.classOpenTime = new LovoDate("开班时间", 20, 140, this);
        this.addBtn = new LovoButton("添加", 40, 200, this);
        this.cancelBtn = new LovoButton("取消", 150, 200, this);

        this.addBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                theClass = new ClassBean();
                String name = ClassAddDialog.this.classTxt.getText();
                String teacher = ClassAddDialog.this.teacherTxt.getText();

                Date time = ClassAddDialog.this.classOpenTime.getDate();

                theClass.setClassName(name);
                theClass.setClassTeacher(teacher);
                theClass.setClassDate(time);

                ClassService classService = context.getBean(ClassServiceImp.class);
                classService.addClass(theClass);

                frame.getClassNode().addListener();
            }
        });
    }

}
